<?php session_start()?>
<?php if (!isset($_GET['lang'])and (!isset($_SESSION['set_lang']))) {
	// se nao tiver nenhuma o default e english
	
	$_SESSION['set_lang'] = "eng-US";	
}elseif (isset($_GET['lang'])and (isset($_SESSION['set_lang']))) {
	
	// se ja tiver a sessao desativa a velha e coloca o novo valor q vem pelo GET
	unset($_SESSION['set_lang']);
	// recolocando o valor
	$_SESSION['set_lang'] = $_GET['lang'];
	
}elseif (isset($_GET['re_set_lang']) and (!isset($_SESSION['set_lang']))){
	
	//se for destruida no logout  vai vir pelo GET home.php?re_set_lang=eng-US
	$_SESSION['set_lang'] = $_GET['re_set_lang'];
}
//testando
$variavel_lang = $_SESSION['set_lang'];
//echo $variavel_lang."<br />";

//sistema de cookie funcionando abaixo
//melhor q usar segundos
//acredito ser 60seg 60min 24horas 30dias 
//In the example above the expiration time is set to a month (60 sec * 60 min * 24 hours * 30 days).
//$expire = time()+60*60*24*1;

//setcookie("cookie", "cookieVALOR2", $expire, "/" );
//echo $_COOKIE["cookie"]. "!<<<<<<<============= AQUI<br />";
//unset($_COOKIE['translate']);
//isso ve todos os cookies
//print_r($_COOKIE);
//isso ve todas as sessoes
//print_r($_SESSION);
?>
<!--/ PART check serial  -->
<?php 
//dataBase
include_once '../transformador/padraoDeLuz.php';
if (isset($_POST['logar']) And $_POST['logar']=="Entrar" ) {
	
	if(strlen($_POST['email_user']) < 4  ){
		
		$error_login = "Email Must Be More Than 4 Characters.";
		//echo "Email Must Be More Than 4 Characters.";
		//usado pra ativar o icone
		$error_email='1';
		
	}//valida  email 
	elseif(!preg_match("/^[a-z0-9_\.\-]+@[a-z0-9_\.\-]*[a-z0-9_\-]+\.[a-z]{2,4}$/i",$_POST['email_user'])){
		
		$error_login = "Email Not valid!";
		//echo'<div class="alert">Email Not valid!!!.</div>';
		//usado pra ativar o icone
		$error_email='1';
	}
	elseif(strlen($_POST['serial']) < 4  ){
		
		$error_login = "Password Must Be More Than 4 Characters.";
		//echo "Password Must Be More Than 4 Characters.";
		//usado pra ativar o icone
		$error_pw='1';
	}//somente senha valida
	elseif(!ctype_alnum($_POST['serial']) // numbers & digits only
			&& !preg_match('`[A-Z]`',$_POST['serial']) // at least one upper case
			&& !preg_match('`[a-z]`',$_POST['serial']) // at least one lower case
			&& !preg_match('`[0-9]`',$_POST['serial']) // at least one digit
			){	
			$error_login = "Password contain caracteres not valid.";
			//echo "Password Must Be More Than 4 Characters.";
			//usado pra ativar o icone
			$error_pw='1';
			}
	
	
	//pega os post
	$user_check_email = mysql_real_escape_string($_POST['email_user']);
	$user_check_pw    = md5($_POST['serial']);
	$ip = $_POST['ip'];
	
	//compara os dados da DB
	$checar = mysql_query("SELECT 'email' 'senha' FROM usuarios WHERE email='$user_check_email' AND senha='$user_check_pw' ")or die (mysql_error());
	//se existir OS dois
	if(mysql_num_rows($checar) == 1 ){
		//tranforma as variaveis nas da sessao
		$_SESSION['userEmailSession'] = $user_check_email;
		//detalhe ela nao ta md5
		$_SESSION['userPasswordSession'] = $user_check_pw;
	
		//usa o email  pra descobrir o id e usar ele como referencia pra pegar o resto das info
		$pega_id = mysql_query("SELECT id FROM usuarios WHERE email='$user_check_email' AND senha='$user_check_pw'")or die (mysql_error());
	
			//monta uma array pra espalhar os dados neste caso so o id
			while($valido_user = mysql_fetch_array($pega_id)){
				//uma array so pra arrancar o id
				$user_id = $valido_user['id'];
			}
			
		//joga i id na sessao tambem
		// e ele vai por sessao pra proxima pagina
		$_SESSION['userIdSession'] = $user_id;
		//aqui redireciona a pagina pro profile
			//echo "<meta http-equiv='refresh' content='1;URL=home.php'>";
		echo 'You\'ll be redirected in about 1 secs. If not, click <a href="home.php">here LOGOU</a>.';
		echo $ip;
		//header( "Location: advertiser/advprof.php");
		//echo 'erro';
	}else {
		// se login errado destroi as sessao alem de jogar de volta pra tela de login
		//desmonta as variaveis
		unset ($_SESSION['userEmailSession']);
		unset ($_SESSION['userPasswordSession']);
		unset ($_SESSION['userIdSession']);
		// desstroy toda a sessao
		session_destroy();
		//redireciona pra pagina de login pra outra tentativa
		//echo "<meta http-equiv='refresh' content='1;URL=advLog.php'>";
		echo 'NAO LOGOU<br />';
		echo $_POST['ip'];	
	}
}
?>




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<?php 
// aqui redireciona para a linguagen
//include_once "lang/".$_SESSION['set_lang']."/home.php";
?>
	
<link rel="stylesheet" href="css/main.css" type="text/css" />
<style type="text/css">
body
	{
/* 	background-image:url('../imagens/fantasy-background.jpg'); */
/* 	background-position:center top; */
/* 	background-repeat: no-repeat; */
	
	}
</style>

<title>Gstv.Inc &copy;&reg;| Login.</title>
</head>

<body>

<div id="login_body">
	
				<?php //include 'includes/header.php';?>
				<?php //include 'includes/navBar.php';?>
				
				
				
				<?php
				if (isset($error_login)){
				echo"<div id=\"errorBox_loginOn\">
							<div id=\"errorBox_text\">$error_login</div>
					</div>";
				}else{
					echo"<div id=\"errorBox_loginOff\"></div>";						
				}
				?>
				
				<div id="login_box"><!-- CONTEUDO PRINCIPAL -->
						<h1>Welcome to Admin!</h1><br />
						Use a valid username and password to gain access to the administration console.<br /><br /><br />
				
				
				
						<form action="index.php" method="post">
						<!-- campo Email -->
							<div class="login_row">Email:
								<input maxlength="30" class="login_field" type="text" title="Email" name="email_user" value="<?php if(isset($_POST['email_user'])){echo $_POST['email_user'];}?>" />
									<div class="error_icon">
										<?php //em caso de error mostra as imagens
										if (isset($error_email) And $error_email =="1" ) {
											echo "<img src=\"../imagens/Critical.png\" alt=\"Error\" />";
										}
										?>
									</div>
							 </div>
						<!-- campo Password -->
							<div class="login_row">Password:
								<input maxlength="30" class="login_field" type="password" title="Password" name="serial" value="" />
									<div class="error_icon">
										<?php //em caso de error mostra as imagens
										if (isset($error_pw) And $error_pw =="1" ) {
											echo "<img src=\"../imagens/Critical.png\" alt=\"Error\" />";
										}
										?>
									</div>
							</div>
							
							<br />
							<input type="hidden" value="<?php echo $_SERVER['REMOTE_ADDR'];?>" name="ip"/>
							<input class="login_btn" type="submit"  title="Clique aqui para logar Obrigado." name="logar"  value="Entrar"/>
						</form>
						
						
						<div id="login_page_news">
							<marquee height="80" onmouseout="this.start()" onmouseover="this.stop()" scrolldelay="20" scrollamount="1" direction="up" align="middle" behavior="scroll">
								<p>Novidades aqui!</p>
								<p>Powered by <a href="#"  title="Bramin e um Admin criado para ser usado em sites PhP [Criado por Gstv.Inc]">Bramin</a> 2010 | 2011,[version RC-0.05]<br />
						 			<a href="mailto:gstv.inc@gmail.com?subject=Comments&body=Tanks" title="Fell Free to email me  ;)">Gstv.Inc&trade;</a> &copy;&reg;&nbsp; All rights reserved.</p>
								<p>Espere ou contacte o Admin Para Mais Inf.</p>
								<p>Gstv.Inc ©®  All rights reserved. </p>
							</marquee></div>
						
				
		
	
			</div><!-- "login_box" -->
		<?php //include 'includes/footer.php';?>
		<p><a href="mailto:gstv.inc@gmail.com?subject=Comments&body=Tanks" title="Fell Free to email me  ;)">Gstv.Inc&trade;</a> &copy;&reg;&nbsp; All rights reserved.<?php echo date('Y');?></p>
	</div>
</body>
</html>
